﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace CustomsSystem.DAL
{
    /// <summary>
    /// 数据访问类:InvoiceService
    /// </summary>
    public partial class InvoiceService
    {
        public InvoiceService()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int invoiceID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_invoice");
            strSql.Append(" where invoiceID=" + invoiceID + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.Invoice model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.sheetID != null)
            {
                strSql1.Append("sheetID,");
                strSql2.Append("" + model.sheetID + ",");
            }
            if (model.invType != null)
            {
                strSql1.Append("invType,");
                strSql2.Append("" + model.invType + ",");
            }
            if (model.invTilte != null)
            {
                strSql1.Append("invTilte,");
                strSql2.Append("'" + model.invTilte + "',");
            }
            if (model.invID != null)
            {
                strSql1.Append("invID,");
                strSql2.Append("'" + model.invID + "',");
            }
            if (model.invAmount != null)
            {
                strSql1.Append("invAmount,");
                strSql2.Append("" + model.invAmount + ",");
            }
            if (model.invEmpID != null)
            {
                strSql1.Append("invEmpID,");
                strSql2.Append("'" + model.invEmpID + "',");
            }
            if (model.invDate != null)
            {
                strSql1.Append("invDate,");
                strSql2.Append("'" + model.invDate + "',");
            }
            if (model.inputEmpNo != null)
            {
                strSql1.Append("inputEmpNo,");
                strSql2.Append("'" + model.inputEmpNo + "',");
            }
            if (model.inputDate != null)
            {
                strSql1.Append("inputDate,");
                strSql2.Append("'" + model.inputDate + "',");
            }
            if (model.remark != null)
            {
                strSql1.Append("remark,");
                strSql2.Append("'" + model.remark + "',");
            }
            strSql.Append("insert into T_invoice(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Invoice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_invoice set ");
            if (model.sheetID != null)
            {
                strSql.Append("sheetID=" + model.sheetID + ",");
            }
            if (model.invType != null)
            {
                strSql.Append("invType=" + model.invType + ",");
            }
            else
            {
                strSql.Append("invType= null ,");
            }
            if (model.invTilte != null)
            {
                strSql.Append("invTilte='" + model.invTilte + "',");
            }
            else
            {
                strSql.Append("invTilte= null ,");
            }
            if (model.invID != null)
            {
                strSql.Append("invID='" + model.invID + "',");
            }
            else
            {
                strSql.Append("invID= null ,");
            }
            if (model.invAmount != null)
            {
                strSql.Append("invAmount=" + model.invAmount + ",");
            }
            else
            {
                strSql.Append("invAmount= null ,");
            }
            if (model.invEmpID != null)
            {
                strSql.Append("invEmpID='" + model.invEmpID + "',");
            }
            else
            {
                strSql.Append("invEmpID= null ,");
            }
            if (model.invDate != null)
            {
                strSql.Append("invDate='" + model.invDate + "',");
            }
            else
            {
                strSql.Append("invDate= null ,");
            }
            if (model.inputEmpNo != null)
            {
                strSql.Append("inputEmpNo='" + model.inputEmpNo + "',");
            }
            else
            {
                strSql.Append("inputEmpNo= null ,");
            }
            if (model.inputDate != null)
            {
                strSql.Append("inputDate='" + model.inputDate + "',");
            }
            else
            {
                strSql.Append("inputDate= null ,");
            }
            if (model.remark != null)
            {
                strSql.Append("remark='" + model.remark + "',");
            }
            else
            {
                strSql.Append("remark= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where invoiceID=" + model.invoiceID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int invoiceID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_invoice ");
            strSql.Append(" where invoiceID=" + invoiceID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string invoiceIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_invoice ");
            strSql.Append(" where invoiceID in (" + invoiceIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Invoice GetModel(int invoiceID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" invoiceID,sheetID,invType,invTilte,invID,invAmount,invEmpID,invDate,inputEmpNo,inputDate,remark ");
            strSql.Append(" from T_invoice ");
            strSql.Append(" where invoiceID=" + invoiceID + "");
            CustomsSystem.Model.Invoice model = new CustomsSystem.Model.Invoice();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Invoice DataRowToModel(DataRow row)
        {
            CustomsSystem.Model.Invoice model = new CustomsSystem.Model.Invoice();
            if (row != null)
            {
                if (row["invoiceID"] != null && row["invoiceID"].ToString() != "")
                {
                    model.invoiceID = int.Parse(row["invoiceID"].ToString());
                }
                if (row["sheetID"] != null && row["sheetID"].ToString() != "")
                {
                    model.sheetID = int.Parse(row["sheetID"].ToString());
                }
                if (row["invType"] != null && row["invType"].ToString() != "")
                {
                    model.invType = int.Parse(row["invType"].ToString());
                }
                if (row["invTilte"] != null)
                {
                    model.invTilte = row["invTilte"].ToString();
                }
                if (row["invID"] != null)
                {
                    model.invID = row["invID"].ToString();
                }
                if (row["invAmount"] != null && row["invAmount"].ToString() != "")
                {
                    model.invAmount = decimal.Parse(row["invAmount"].ToString());
                }
                if (row["invEmpID"] != null)
                {
                    model.invEmpID = row["invEmpID"].ToString();
                }
                if (row["invDate"] != null && row["invDate"].ToString() != "")
                {
                    model.invDate = DateTime.Parse(row["invDate"].ToString());
                }
                if (row["inputEmpNo"] != null)
                {
                    model.inputEmpNo = row["inputEmpNo"].ToString();
                }
                if (row["inputDate"] != null && row["inputDate"].ToString() != "")
                {
                    model.inputDate = DateTime.Parse(row["inputDate"].ToString());
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select invoiceID,sheetID,invType,invTilte,invID,invAmount,invEmpID,invDate,inputEmpNo,inputDate,remark ");
            strSql.Append(" FROM T_invoice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" invoiceID,sheetID,invType,invTilte,invID,invAmount,invEmpID,invDate,inputEmpNo,inputDate,remark ");
            strSql.Append(" FROM T_invoice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_invoice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.invoiceID desc");
            }
            strSql.Append(")AS Row, T.*  from T_invoice T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  Method
        #region  MethodEx

        #endregion  MethodEx
    }
}

